import 'package:example/pages/animation_demo_page.dart';
import 'package:example/pages/button_demo_page.dart';
import 'package:example/pages/gesture_demo_page.dart';
import 'package:example/pages/layout_build_demo_page.dart';
import 'package:example/pages/leaf_render_widget_page.dart';
import 'package:example/pages/listener_demo_page.dart';
import 'package:example/pages/navigation_view_page.dart';
import 'package:example/pages/route_page_demo.dart';
import 'package:example/pages/scrollview_demo_page.dart';
import 'package:example/pages/tabbar_demo_page.dart';
import 'package:example/pages/table_view_page.dart';
import 'package:example/pages/template_edit_page.dart';
import 'package:example/pages/text_drop_demo_page.dart';
import 'package:example/pages/viewmodel_page.dart';
import 'package:example/pages/text_demo_page.dart';
import 'package:example/pages/webview_demo_page.dart';
import 'package:example/widget/custom_wrap_widget.dart';
import 'package:example/widget/flow_delegate_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_scaffold/flutter_scaffold.dart';

import 'db/app_database.dart';
import 'deeplink/deep_link_service.dart';
import 'generated/l10n.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Get.locale = Get.deviceLocale;
  await FlutterScaffold.init();
  Log.i("===>${Get.deviceLocale?.toString()}");
  Future.value(AppDatabase.instance);
  Future.value(DeepLinkService().initDeepLinks());
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return AppWidget(
      systemUiOverlayStyle: SystemUiOverlayStyle.light.copyWith(systemNavigationBarColor: Colors.white),
      designSize: const Size(360, 760),
      child: GetMaterialApp(
        initialRoute: "/",
        locale: Get.locale,
        localizationsDelegates: const [...GlobalMaterialLocalizations.delegates, S.delegate],
        supportedLocales: S.delegate.supportedLocales,
        title: 'Flutter Demo',
        navigatorObservers: [RouteObserverUtil().routeObserver],
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          textButtonTheme: TextButtonThemeData(
            style: ButtonStyle(backgroundColor: WidgetStatePropertyAll(Colors.greenAccent)),
          ),
          useMaterial3: true,
        ),
        home: const MyHomePage(title: 'Flutter Demo Home Page'),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    SchedulerBinding.instance.addPostFrameCallback((temptimes) {
      Log.i(temptimes);
    });
  }

  @override
  Widget build(BuildContext context) {
    print("APP->状态:build");
    return Scaffold(
      appBar: AppBar(backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title)),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: SingleChildScrollView(
          child: Wrap(
            spacing: 10.w,
            children: [
              TextButton(
                style: ButtonStyle(backgroundColor: WidgetStatePropertyAll(ColorChart.red)),
                onPressed: () {
                  Get.to(() => ViewModelDemoPage());
                },
                child: Text("*ViewModel 使用*", style: TextStyle(color: Colors.white)),
              ),
              TextButton(
                style: ButtonStyle(backgroundColor: WidgetStatePropertyAll(ColorChart.red)),
                onPressed: () {
                  Get.to(TemplateEditPage());
                },
                child: Text("* 模版编辑 *", style: TextStyle(color: Colors.white)),
              ),
              TextButton(
                onPressed: () {
                  Get.to(TabBarDemoPage());
                },
                child: Text("TabBar使用"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(NavigationViewPage());
                },
                child: Text("底部导航栏"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(TableExample());
                },
                child: Text("表格使用"),
              ),
              TextButton(
                onPressed: () {
                  //方法一：Get 方式
                  Get.to(() => RoutePageDemo(), arguments: {"title": "黄智波"})?.then((vaue) {
                    Log.i("接受参数:${vaue}");
                  });
                },
                child: Text("路由跳转参数传递"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(() => TextDemoPage());
                },
                child: Text("Text 详细使用"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(() => ButtonDemoPage());
                },
                child: Text("Button 详细使用"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(() => LeafRenderWidgetPage());
                },
                child: Text("自定义子节点widget"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(() => LayoutBuildDemoPage());
                },
                child: Text("layoutBuild 使用"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(() => ScrollViewDemoPage());
                },
                child: Text("ScrollView 使用"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(() => ListenerDemoPage());
                },
                child: Text("Listener 使用"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(() => GestureDemoPage());
                },
                child: Text("Gesture 使用"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(() => AnimationDemoPage());
                },
                child: Text("Animation 使用"),
              ),
              TextButton(
                onPressed: () {
                  Get.updateLocale(Locale(Get.locale?.languageCode == "zh" ? 'en' : 'zh'));
                },
                child: Text("切换语言:${S.current.title}"),
              ),
              TextButton(
                onPressed: () async {
                  final db = await AppDatabase.instance;
                  //db.personDao.insertPerson(Person(name: "zhibo.huang"));
                  var persons = await db.personDao.findAllPeople();
                  Log.i(persons);
                },
                child: Text(S.current.sql_use),
              ),
              TextButton(
                onPressed: () {
                  Get.to(TextDropDemoPage());
                },
                child: Text("文本拖动"),
              ),

              TextButton(
                onPressed: () {
                  Get.to(WebviewDemoPage());
                },
                child: Text("webView"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(CustomWrapWithTextMeasureDemo());
                },
                child: Text("flow自定义delegate"),
              ),
              TextButton(
                onPressed: () {
                  Get.to(
                    Scaffold(
                      appBar: AppBar(),
                      body: Container(
                        color: Colors.red,
                        child: CustomWrap(
                          children: [
                            Text("data范德萨范德萨发斯蒂芬递四方速递范德萨范德萨1"),
                            Icon(Icons.add),
                            Text("data范德萨范德萨发斯蒂芬递四方速递范德萨范德萨1"),
                          ],
                        ),
                      ),
                    ),
                  );
                },
                child: Text("自定义wrap效果"),
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Log.i("当前app 语言:" + Get.locale!.languageCode);
          Get.updateLocale(Locale(Get.locale?.languageCode == "zh" ? 'en' : 'zh'));
        },
        child: Icon(Icons.language),
      ),
    );
  }
}
